package com.htfyun.roobofactory.factorytest;

import android.os.BatteryManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.htfyun.roobofactory.BaseActivity;
import com.htfyun.roobofactory.R;
import com.htfyun.roobofactory.config.RooboBuildConfig;
import com.htfyun.roobofactory.htfyunUtils.BatteryInfo;

/**
 * Created by sst on 2018/7/9.
 */

public class BatteryTestActivity extends BaseActivity {

    private TextView txtBattery;
    private Button btnPass;
    private Button btnFail;

    private BatteryInfo batteryInfo;

    private int previousBatteryStatus = -1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_factorytest_battery);

        txtBattery = (TextView) findViewById(R.id.txtBattery);

        btnPass = (Button) findViewById(R.id.btnPass);
        btnFail = (Button) findViewById(R.id.btnFail);
        btnPass.setOnClickListener(onPassOrFailClickListener);
        btnFail.setOnClickListener(onPassOrFailClickListener);

        btnPass.setVisibility(RooboBuildConfig.FT_ADD_PASS_BTN ? View.VISIBLE : View.GONE);

        batteryInfo = new BatteryInfo(this);

    }

    @Override
    protected void onResume() {
        super.onResume();
        batteryInfo.startMonitor();
        batteryInfo.setOnBatteryChangedListener(batteryChangedListener);

    }

    @Override
    protected void onPause() {
        super.onPause();
        batteryInfo.setOnBatteryChangedListener(null);
        batteryInfo.stopMonitor();
    }

    private BatteryInfo.OnBatteryChangedListener batteryChangedListener =
            new BatteryInfo.OnBatteryChangedListener() {
                @Override
                public void onBatteryChangedListener() {

                    txtBattery.setText(getString(R.string.test_battery_charge_status, batteryInfo.getBatteryStatusString()));

                    int status = batteryInfo.getBatteryStatus();

                    if (previousBatteryStatus > 0
                            && previousBatteryStatus != BatteryManager.BATTERY_STATUS_UNKNOWN
                            && status != BatteryManager.BATTERY_STATUS_UNKNOWN
                            && previousBatteryStatus != status) {

                        startAutoPass(R.string.test_battery_pass);

                    }


                    previousBatteryStatus = status;

                }
            };


    private void setBtnPassOrFailEnabled(boolean enabled) {
        btnFail.setEnabled(enabled);
        btnPass.setEnabled(enabled);
    }

    private View.OnClickListener onPassOrFailClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v == btnPass) {

                setResult(RESULT_OK);
                ActivityCompat.finishAfterTransition(getActivity());

            } else if (v == btnFail) {

                setResult(RESULT_CANCELED);
                ActivityCompat.finishAfterTransition(getActivity());

            }
        }
    };
}
